package com.itheima.cs.dao;

import com.itheima.cs.entity.Userinfo;
import com.itheima.cs.help.DbOpenHelper;

public class UserDao extends DbOpenHelper {
    public Userinfo getUserByUsernameAndPassword(String username, String password){
        Userinfo item = null;
        try{
            getConnection();
            String sql = "select * from [dbo].[user] where username=? and password=?";
            pStmt = conn.prepareStatement(sql);
            pStmt.setString(1,username);
            pStmt.setString(2,password);
            rs = pStmt.executeQuery();
            if(rs.next()){
                item = new Userinfo();
                item.setId(rs.getInt("id"));
                item.setUsername(rs.getString("username"));
                item.setPassword(password);
                item.setEmpNumber(rs.getString("emp_number"));
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            closeAll();
        }
        return item;
    }


    public Userinfo getUserByUsername(String username) {
        Userinfo item = null;
        try {
            getConnection();
            String sql = "SELECT * FROM [dbo].[user] WHERE username=?";
            pStmt = conn.prepareStatement(sql);
            pStmt.setString(1, username);
            rs = pStmt.executeQuery();
            if (rs.next()) {
                item = new Userinfo();
                item.setId(rs.getInt("id"));
                item.setUsername(rs.getString("username"));
                item.setPassword(rs.getString("password"));
                item.setPhone(rs.getString("phone"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return item;
    }


    public boolean updateUserPassword(String username, String newPassword) {
        boolean isUpdated = false;
        try {
            getConnection();
            String sql = "UPDATE [dbo].[user] SET password=? WHERE username=?";
            pStmt = conn.prepareStatement(sql);
            pStmt.setString(1, newPassword);
            pStmt.setString(2, username);
            int rowsAffected = pStmt.executeUpdate();
            isUpdated = rowsAffected > 0;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return isUpdated;
    }

    public Userinfo getUserById(int userId) {
        Userinfo item = null;
        try {
            getConnection();
            String sql = "SELECT * FROM [dbo].[user] WHERE id=?";
            pStmt = conn.prepareStatement(sql);
            pStmt.setInt(1, userId);
            rs = pStmt.executeQuery();
            if (rs.next()) {
                item = new Userinfo();
                item.setId(rs.getInt("id"));
                item.setName(rs.getString("name"));
                item.setUsername(rs.getString("username"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeAll();
        }
        return item;
    }
}
